#ifndef XLA_CLIENT_DEBUG_MACROS_H_
#define XLA_CLIENT_DEBUG_MACROS_H_

#include "absl/status/status.h"
#include "tsl/platform/stacktrace.h"
#include "tsl/platform/statusor.h"

#include "torch_xla/csrc/runtime/tf_logging.h"

// DEPRECATED
// ==========
// These macros are deprecated in favor of error handling by propagating abseil
// status types (e.g. `absl::Status` and `absl::StatusOr<T>`).
//
// Description
// ===========
// TORCH_SHOW_CPP_STACKTRACES environment variable changes the behavior of the
// macros below, such as XLA_CHECK(), XLA_CHECK_EQ(), etc. (except for
// XLA_CHECK_OK) in the following way:
//
// If TORCH_SHOW_CPP_STACKTRACES environment variable is set, error messages
// generated by these macros will include detailed source location information
// (file name and line number) and a C++ stacktrace, providing a comprehensive
// context for debugging.
//
// Otherwise, only the concise error message will be displayed, without the
// additional source location or stacktrace details. This allows for cleaner
// error output in production environments where a full stacktrace might be
// unnecessary or undesirable.
#define XLA_ERROR() TF_ERROR_STREAM()
#define XLA_CHECK(c) TF_CHECK(c)
#define XLA_CHECK_EQ(a, b) TF_CHECK_EQ(a, b)
#define XLA_CHECK_NE(a, b) TF_CHECK_NE(a, b)
#define XLA_CHECK_LE(a, b) TF_CHECK_LE(a, b)
#define XLA_CHECK_GE(a, b) TF_CHECK_GE(a, b)
#define XLA_CHECK_LT(a, b) TF_CHECK_LT(a, b)
#define XLA_CHECK_GT(a, b) TF_CHECK_GT(a, b)

template <typename T>
T ConsumeValue(absl::StatusOr<T>&& status) {
  XLA_CHECK_OK(status.status());
  return std::move(status).value();
}

#endif  // XLA_CLIENT_DEBUG_MACROS_H_
